package com.toogu.mazhangmen.util;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.toogu.mazhangmen.MyApplication;
import com.toogu.mazhangmen.bean.DoorReturnBean;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class NetWorkUtil {

    private volatile static NetWorkUtil netWorkUtil;

    public static NetWorkUtil getNetWorkUtil() {
        if (netWorkUtil == null) {
            synchronized (OkhttpUtil.class) {
                if (netWorkUtil == null) {
                    netWorkUtil = new NetWorkUtil();
                }
            }
        }
        return netWorkUtil;
    }

    private NetWorkUtil() {
    }

    public void pushQAType(Activity activity,String doorId,String problemId,String reason,Callback callback){
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("door_id",doorId );
        builder.add("problem_id",problemId);
        builder.add("reason",reason );
        Log.d("TAG", "pushQAType: "+doorId+" "+problemId+" "+reason);
        OkhttpUtil.getOkhttpUtil().sentPost(activity,"/api/duty/submit-problem", builder.build(), callback);
    }


    public void getQAType(Activity activity,Callback callback){
        OkhttpUtil.getOkhttpUtil().sentGet(activity,"/api/duty/problem-type-options",callback);
    }


    //获取停车费用
    public void getTempRecord(Activity activity,String mobile,String project_id,Callback callback){
        Log.d("getTempRecord", "getTempRecord: "+mobile+" "+project_id);
        OkhttpUtil.getOkhttpUtil().sentGet(activity,"/api/duty/helper/temp-record?mobile="+mobile+"&project_id="+project_id,callback);
    }

    //轮询出入记录
    public void getAccessOutRecord(Activity activity,String projectId,String doorId,Callback callback){
        OkhttpUtil.getOkhttpUtil().sentGet(activity,"/api/duty/project/"+projectId+"/access-out-record?door_id="+doorId,callback);
    }

    //详细情况
    public void getProject(Activity activity,String projectId, Callback callback) {
        OkhttpUtil.getOkhttpUtil().sentGet(activity,"/api/duty/project/" + projectId, callback);
    }

    //获取所有项目列表
    public void getProjectList(Activity activity,Callback callback){
        OkhttpUtil.getOkhttpUtil().sentGet(activity,"/api/duty/project?per-page=10000",callback);
    }

    //登录
    public void AuthToken(Activity activity,String username, String password, Callback callback) {
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("username", username);
        builder.add("password", password);

        OkHttpClient okHttpClient = OkhttpUtil.getOkhttpUtil().getOkHttpClient();
        String url = OkhttpUtil.urlHead + "/api/duty/auth/token";
        Request request = new Request.Builder()
                .addHeader("Content-Type", "application/json")
                .url(url).post(builder.build()).build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    //用户基本信息
    public void getUserInfo(Activity activity, Callback callback) {
        OkhttpUtil.getOkhttpUtil().sentGet(activity,"/api/duty/auth/info", callback);
    }

    //创建临时号码
    public void createTempMobile(Activity activity,Callback callback) {
        OkhttpUtil.getOkhttpUtil().sentGet(activity,"/api/duty/helper/create-temporary-mobile", callback);
    }
    //设备的详细信息
    public void equipmentDetail(Activity activity, String id,Callback callback) {
        OkhttpUtil.getOkhttpUtil().sentGet(activity,"/api/duty/project/" + id + "/equipment-detail",callback);
    }

    //收费
    public void putTemporaryConfirm(Activity activity,String temporary_record_id){
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("temporary_record_id",temporary_record_id);
        Log.d("putTemporaryConfirm", "putTemporaryConfirm: "+temporary_record_id);
        OkhttpUtil.getOkhttpUtil().sentPut(activity,"/api/duty/helper/temporary-confirm",builder.build(),new DoorCallback(activity,""));
    }

    //半开门
    public void halfOpen(Activity activity, String doorId, boolean isAuto,String name) {
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("auto", isAuto ? "1" : "0");
        String action = isAuto?"开半门(自动)":"开半门(手动)";
        OkhttpUtil.getOkhttpUtil().sentPut(activity,"/api/duty/door/" + doorId + "/half-open", builder.build(), new DoorCallback(activity,name+action));
    }

    //全开门
    public void allOpen(Activity activity, String doorId, boolean isAuto,String name) {
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("auto", isAuto ? "1" : "0");
        String action = isAuto?"开全门(自动)":"开全门(手动)";
        OkhttpUtil.getOkhttpUtil().sentPut(activity,"/api/duty/door/" + doorId + "/all-open", builder.build(), new DoorCallback(activity,name+action));
    }

    //关门
    public void close(Activity activity, String doorId,String name) {
        FormBody.Builder builder = new FormBody.Builder();
        OkhttpUtil.getOkhttpUtil().sentPut(activity,"/api/duty/door/" + doorId + "/close", builder.build(), new DoorCallback(activity,name+"关门"));
    }

    //门的回调
    class DoorCallback implements Callback {
        private Activity activity;
        private String action;
        public DoorCallback(Activity activity,String action){
            this.action=action;
            this.activity=activity;
        }
        public void onFailure(Call call, IOException e) {
            if(activity==null) return;
            activity.runOnUiThread(()->Toast.makeText(MyApplication.getContext(),"连接失败:"+e.getMessage(),Toast.LENGTH_SHORT).show());
        }
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if(activity==null) return;
            String responseText=response.body().string();
            Log.d("OkCallback", "onResponse: "+response+responseText);
            DoorReturnBean doorReturnBean = new Gson().fromJson(responseText, DoorReturnBean.class);
//            activity.runOnUiThread(()->Toast.makeText(activity,action+":"+doorReturnBean.getMessage(),Toast.LENGTH_SHORT).show());
            ToastUtil.getToastUtil().add(action+":\n"+doorReturnBean.getMessage());
        }
    }


}